package com.bigstar.lolkor;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

public class Option extends Activity {
	
	boolean sound;
	boolean image_save;
	boolean illust_us;
	boolean notice;
    @Override
    protected void onResume() {
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
    	super.onResume();
    }

    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        ChampionData.load(this);
        ItemData.load(this);
        Rune.load(this);
		setContentView(R.layout.option);
        AdView adview = (AdView)findViewById(R.id.adView);
        AdRequest re = new AdRequest(); 
        adview.loadAd(re);
        
        
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		sound = pref.getBoolean("sound", true);
		image_save = pref.getBoolean("image_save", true);
		illust_us = pref.getBoolean("illust_us", true);
		notice = pref.getBoolean("notice", false);
        
		RadioGroup rd = (RadioGroup) this.findViewById(R.id.sound_radio_group);
		if(sound) {
			RadioButton b = (RadioButton)findViewById(R.id.sound_on);
			b.setChecked(true);
		}else{
			RadioButton b = (RadioButton)findViewById(R.id.sound_off);
			b.setChecked(true);
		}
		rd.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				if (id == R.id.sound_on) {
					sound = true;
				} else {
					sound = false;
				}
				try {
					save(Option.this);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		
		
		rd = (RadioGroup) this.findViewById(R.id.image_save_radio_group);
		if(image_save) {
			RadioButton b = (RadioButton)findViewById(R.id.image_save_on);
			b.setChecked(true);
		}else{
			RadioButton b = (RadioButton)findViewById(R.id.image_save_off);
			b.setChecked(true);
		}
		rd.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				if (id == R.id.image_save_on) {
					image_save = true;
				} else {
					image_save = false;
				}
				try {
					save(Option.this);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		
		
		rd = (RadioGroup) this.findViewById(R.id.language_radio_group);
		if(illust_us) {
			RadioButton b = (RadioButton)findViewById(R.id.language_on);
			b.setChecked(true);
		}else{
			RadioButton b = (RadioButton)findViewById(R.id.language_off);
			b.setChecked(true);
		}
		rd.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				if (id == R.id.language_on) {
					illust_us = true;
				} else {
					illust_us = false;
				}
				try {
					save(Option.this);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		
		rd = (RadioGroup) this.findViewById(R.id.notice_radio_group);
		if(notice) {
			RadioButton b = (RadioButton)findViewById(R.id.notice_on);
			b.setChecked(true);
		}else{
			RadioButton b = (RadioButton)findViewById(R.id.notice_off);
			b.setChecked(true);
		}
		rd.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup arg0, int id) {
				if (id == R.id.notice_on) {
					notice = true;
				} else {
					notice = false;
				}
				try {
					save(Option.this);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		
		Button b = (Button)findViewById(R.id.btn_skin_del);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new AlertDialog.Builder(Option.this)
				.setTitle("스킨 삭제")
				.setMessage("스킨 데이터를 삭제 하시겠습니까?")
				.setPositiveButton("삭제", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						deleteSkin();
					}
				})
				.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				})
				.create().show();
			}
		});
		
		b = (Button)findViewById(R.id.btn_illust_del);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new AlertDialog.Builder(Option.this)
				.setTitle("일러스트 삭제")
				.setMessage("일러스트 데이터를 삭제 하시겠습니까?")
				.setPositiveButton("삭제", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						deleteIllust();
					}
				})
				.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				})
				.create().show();
			}
		});
		
		b = (Button)findViewById(R.id.btn_3d_del);
		b.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				new AlertDialog.Builder(Option.this)
				.setTitle("3D 모델러 삭제")
				.setMessage("3D 모델러 데이터를 삭제 하시겠습니까?")
				.setPositiveButton("삭제", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						String str = Environment.getExternalStorageState();
						if ( str.equals(Environment.MEDIA_MOUNTED)) {
							File f = new File("/sdcard/Android/data/LOLDATA/3d/");
							delete3D(f);
							Toast.makeText(Option.this, "3D 모델러 삭제 성공", Toast.LENGTH_SHORT).show();	
						}
					}
				})
				.setNegativeButton("닫기", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
					}
				})
				.create().show();
			}
		});
	}

	public void save(Context context) throws IOException {
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean("sound", sound);
		editor.putBoolean("image_save", image_save);
		editor.putBoolean("illust_us", illust_us);
		editor.putBoolean("notice", notice);
		editor.commit();
	}
	
	public void deleteSkin(){
		String str = Environment.getExternalStorageState();
		if ( str.equals(Environment.MEDIA_MOUNTED)) {
			String dirPath = "/sdcard/Android/data/LOLDATA/skin";
			File dir = new File(dirPath);
			if(!dir.exists()){
				dir.mkdirs();	
			}
			File file = new File(dirPath);
			File[] childFileList = file.listFiles();
			for(File childFile : childFileList){
				childFile.delete();   
			}
			Toast.makeText(this, "스킨 삭제 성공", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void deleteIllust(){
		String str = Environment.getExternalStorageState();
		if ( str.equals(Environment.MEDIA_MOUNTED)) {
			String dirPath = "/sdcard/Android/data/LOLDATA/illust";
			File dir = new File(dirPath);
			if(!dir.exists()){
				dir.mkdirs();	
			}
			File file = new File(dirPath);
			File[] childFileList = file.listFiles();
			for(File childFile : childFileList){
				childFile.delete();   
			}
			Toast.makeText(this, "일러스트 삭제 성공", Toast.LENGTH_SHORT).show();
		}
	}
	
	public int delete3D(File f){  
		File[] file=f.listFiles();
		for(int i=0;i<file.length;i++){
       
			if(file[i].isFile()){//파일이면 다음으로 넘어간다. 
				file[i].delete();  
           
            }else{                 
            	delete3D(file[i]);
            }
           file[i].delete();
        }   
        
        return 1;
    }

}
